package com.gopay.scheduler.job;

import com.gopay.common.constants.batch.BatchProcessStatus;
import com.gopay.common.constants.cbrmb.CbRmb01720OptStep;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.cps.cbrmb.Tran01720VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc: 跨境人民币进口批量支付异步账务处理Job</p>
 * @Desc: 目前是一分钟执行一次
 * @Class: CbRmbBatchImportJob
 * @Author: b.zou</p>
 * @Date: 2017-8-25
 */
public class CbRmbBatchImportJob extends GopayBaseJob {

    private Logger logger = LoggerFactory.getLogger(CbRmbBatchImportJob.class);

    /**
     * 定时任务每隔1分钟执行一次
     */
    private int intervalMinite;

    /**
     * 公共查询Dao
     */
    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;

    /**
     * 访问CPS辅助类
     */
    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Override
    protected void doJobInternal() {
        {


            // 查询等待执行异步账务的汇款拆分订单
            List<String> taskList = queryWaitingAcpsBatchTasks();
            logger.info("CbRmbBatchImportJob taskList size :"+taskList.size());

            if (taskList != null && taskList.size() > 0) {

                Object paramVo = null;
                CPSResponse response;
                for (String orderId : taskList) {
                    paramVo = null;

                    // 汇款拆分异步做账
                    paramVo = build01720BatchParamVo(orderId);

                    if (paramVo != null) {
                        try {
                            logger.error("-------orderId="+orderId);
                            // 调用cbrcps
                            response=callCPSHelper.callCbRmbByVOTicket(paramVo);
                            if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                                logger.error("------调用cbrcps返回异常  response="+response.getRespCode());
                            }

                        } catch (Exception e) {
                            // 吃掉所有异常
                            logger.error("callCbRmbByVOTicket Error", e);
                        }
                    }
                }

                logger.error("complete task num="+taskList.size());
            }

        }
    }

    /**
     * 查询等待执行异步账务的汇款支付总订单
     * bRmbBatchImportJob
     * @return
     */
    private List<String> queryWaitingAcpsBatchTasks() {
        List<String> list = new ArrayList<String>();

        StringBuilder sb = new StringBuilder();

        sb.append(" select t.gopay_order_id,t.mer_txn_amt from cps_gen_main_order t ");
        sb.append("  left join cb_rmb_batch_import_assist c on t.gopay_order_id=c.gopay_order_id ");
        sb.append("  where t.gopay_int_txn_cd= :gopayInTxcd ");
        sb.append("  and c.batch_process_status= :batchStatus ");


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("gopayInTxcd", IntTxnCd._01738.value);
        map.put("batchStatus", BatchProcessStatus.WAITING_ASYNCHRONOUS_ACPS.value);


        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        String r = null;
        if (rsList != null && rsList.size() > 0) {
            for (Object[] arr : rsList) {
                r=(String) arr[0];

                list.add(r);
            }
        }

        return list;
    }

    /**
     * 构造进口异步做账请求参数VO
     * @param orderId
     * @return
     */
    private Object build01720BatchParamVo(String orderId) {
        Tran01720VO paramVo = new Tran01720VO();
        paramVo.setCbRmb01720OptStep(CbRmb01720OptStep.ASYNCHRONOUS);
        paramVo.setGopayOrderId(orderId);
        return paramVo;
    }


    public int getIntervalMinite() {
        return intervalMinite;
    }

    public void setIntervalMinite(int intervalMinite) {
        this.intervalMinite = intervalMinite;
    }
}
